package br.edu.ufcg.dsc.actions.unit;

import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletRedirectResult;

import br.edu.ufcg.dsc.actions.BaseAdminAction;
import br.edu.ufcg.dsc.domain.Unit;
import br.edu.ufcg.dsc.services.LibraryService;
import br.edu.ufcg.dsc.services.ResourceService;
import br.edu.ufcg.dsc.services.UnitService;
import br.edu.ufcg.dsc.services.UserService;

@Result(name="success", type=ServletRedirectResult.class, value="/index.action?message=Unit Successfully Created.")
@ParentPackage("myPackage")
public class UpdateUnitAction extends BaseAdminAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int resourceId;
	private String librarianLogin, ownerLogin, libraryName; 
	private String id;
	
	
	@SuppressWarnings("static-access")
	@Override
	protected String executeIfAdmin() {
		Unit unit = new Unit();
		unit.setResource(new ResourceService().find(resourceId));
		unit.setId(id);
		unit.setLibrary(LibraryService.find(libraryName));
		unit.setOwner(UserService.find(ownerLogin));
		unit.setLibrarian(UserService.find(librarianLogin));
		UnitService.persist(unit);
		
		return SUCCESS;
	}

	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}

	public String getLibraryName() {
		return libraryName;
	}

	public void setLibraryName(String libraryName) {
		this.libraryName = libraryName;
	}

	public void setLibrarianLogin(String librarianLogin) {
		this.librarianLogin = librarianLogin;
	}

	public void setOwnerLogin(String ownerLogin) {
		this.ownerLogin = ownerLogin;
	}
}
